## Replication files for "Do Online Ads Influence Vote Choice?"
## anselm.rink@gmail.com
## This code replicates Table 5

rm(list = ls())
options(scipen=999)
set.seed(492960) #taken from random.org
library(foreign)
library(stargazer)
setwd("") ## set WD
data <- read.csv("data.csv") 


## define robust SE function 
cluster <- function(dat,fm, cluster){
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M/(M-1))*((N-1)/(N-K))
  uj  <- apply(estfun(fm), 2 , function(x) tapply(x, cluster, sum, na.rm=T));
  vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
  coeftest(fm, vcovCL) 
}


## run OLS models
model1 <- cluster(data, lm(cdu16erst ~ treatment_rational + treatment_emotional + age6_15 + age15_18 + age18_27 + age27_45 + age45_55 + age55_65 + age_o65 + female + protestant + catholic + atheist_pop + foreign_pop + total_pop_ln + cdu11erst, data=data), data$PLZ)
model2 <- cluster(data, lm(linke16erst ~ treatment_rational + treatment_emotional + age6_15 + age15_18 + age18_27 + age27_45 + age45_55 + age55_65 + age_o65 + female + protestant + catholic + atheist_pop + foreign_pop + total_pop_ln + linke11erst, data=data), data$PLZ)
model3 <- cluster(data, lm(gruene16erst ~ treatment_rational + treatment_emotional + age6_15 + age15_18 + age18_27 + age27_45 + age45_55 + age55_65 + age_o65 + female + protestant + catholic + atheist_pop + foreign_pop + total_pop_ln + gruene11erst, data=data), data$PLZ)
model4 <- cluster(data, lm(spd16erst ~ treatment_rational + treatment_emotional + age6_15 + age15_18 + age18_27 + age27_45 + age45_55 + age55_65 + age_o65 + female + protestant + catholic + atheist_pop + foreign_pop + total_pop_ln + spd11erst, data=data), data$PLZ)
model5 <- cluster(data, lm(fdp16erst ~ treatment_rational + treatment_emotional + age6_15 + age15_18 + age18_27 + age27_45 + age45_55 + age55_65 + age_o65 + female + protestant + catholic + atheist_pop + foreign_pop + total_pop_ln + fdp11erst, data=data), data$PLZ)
model6 <- cluster(data, lm(afd16erst ~ treatment_rational + treatment_emotional + age6_15 + age15_18 + age18_27 + age27_45 + age45_55 + age55_65 + age_o65 + female + protestant + catholic + atheist_pop + foreign_pop + total_pop_ln, data=data), data$PLZ)
model7 <- cluster(data, lm(data$voters_erst_16 ~ treatment_rational + treatment_emotional + age6_15 + age15_18 + age18_27 + age27_45 + age45_55 + age55_65 + age_o65 + female + protestant + catholic + atheist_pop + foreign_pop + total_pop_ln, data=data), data$PLZ)

## print models
stargazer(model1, model2, model3, model4, model5, model6, model7)

